﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using Lenovo.CFI.BLL;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;

namespace Lenovo.CFI.Web.WS
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class IdeaReview : System.Web.Services.WebService
    {
        ReviewBl reviewBl = new ReviewBl();
        [WebMethod(true)]
        public string SaveCV(int reviewID, string reviewer, string cvText)
        {
            try
            {
                decimal cv = 0;

                reviewBl.Review(reviewID, reviewer, UserHelper.UserName, 
                    (decimal.TryParse(cvText, out cv)) ? cv : -1, 
                    null, null, null, null, null, null, null, null);
            }
            catch (ApplicationException ex)
            {
                return ex.Message;
            }

            return null;
        }

        [WebMethod(true)]
        public string SaveBV(int reviewID, string reviewer, string bvText)
        {
            try
            {
                decimal bv = 0;

                reviewBl.Review(reviewID, reviewer, UserHelper.UserName,
                    null, 
                    (decimal.TryParse(bvText, out bv)) ? bv : -1,
                    null, null, null, null, null, null, null);
            }
            catch (ApplicationException ex)
            {
                return ex.Message;
            }

            return null;
        }

        [WebMethod(true)]
        public string SaveTV(int reviewID, string reviewer, string tvText)
        {
            try
            {
                decimal tv = 0;

                reviewBl.Review(reviewID, reviewer, UserHelper.UserName,
                    null, null, 
                    (decimal.TryParse(tvText, out tv)) ? tv : -1,
                    null, null, null, null, null, null);
            }
            catch (ApplicationException ex)
            {
                return ex.Message;
            }

            return null;
        }

        [WebMethod(true)]
        public string SaveType(int reviewID, string reviewer, string type)
        {
            try
            {
                reviewBl.Review(reviewID, reviewer, UserHelper.UserName,
                    null, null, null,
                    type,
                    null, null, null, null, null);
            }
            catch (ApplicationException ex)
            {
                return ex.Message;
            }

            return null;
        }

        [WebMethod(true)]
        public string SaveComment(int reviewID, string reviewer, string comment)
        {
            try
            {
                reviewBl.Review(reviewID, reviewer, UserHelper.UserName,
                    null, null, null, null,  
                    comment,
                    null, null, null, null);
            }
            catch (ApplicationException ex)
            {
                return ex.Message;
            }

            return null;
        }

        [WebMethod(true)]
        public string SaveIP(int reviewID, string reviewer, string ipText)
        {
            try
            {
                int ip = -1;

                reviewBl.Review(reviewID, reviewer, UserHelper.UserName,
                    null, null, null, null, null,
                    (int.TryParse(ipText, out ip)) ? ip : -1,
                    null, null, null);
            }
            catch (ApplicationException ex)
            {
                return ex.Message;
            }

            return null;
        }

        [WebMethod(true)]
        public string SaveIPComment(int reviewID, string reviewer, string comment)
        {
            try
            {
                reviewBl.Review(reviewID, reviewer, UserHelper.UserName,
                    null, null, null, null, null, null,
                    comment,
                    null, null);
            }
            catch (ApplicationException ex)
            {
                return ex.Message;
            }

            return null;
        }

        [WebMethod(true)]
        public string SaveSuggestion(int reviewID, string reviewer, string suggestion)
        {
            try
            {
                reviewBl.Review(reviewID, reviewer, UserHelper.UserName,
                    null, null, null, null, null, null, null,
                    suggestion,
                    null);
            }
            catch (ApplicationException ex)
            {
                return ex.Message;
            }

            return null;
        }
    }
}
